package com.javaProject.controller;

import com.javaProject.dto.R;
import com.javaProject.entity.Teacher;
import com.javaProject.service.TeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;

@CrossOrigin
@Tag(name="教师接口",description = "这个接口的目的主要用于增删改查")
@RestController
public class teacherController{
    @Autowired
    private TeacherService teacherService;

    //登陆
    @Operation(summary = "教师登陆",description = "需要提供教师的用户名和密码")
    @Parameters({
            @Parameter(name = "username",description = "教师账号",required = true),
            @Parameter(name = "password",description = "教师密码",required = true)
    })
    @GetMapping("/teacher")
    public R login(String username,String password) throws NoSuchAlgorithmException, InvalidKeySpecException {
        Teacher teacher = new Teacher();
        teacher.setUsername(username);
        teacher.setPassword(password);
        return teacherService.login(teacher);
    }



    @Operation(summary = "教师新增",description = "至少提供账号和密码")
    @Parameters({
            @Parameter(name = "username",description = "教师账号",required = true),
            @Parameter(name = "password",description = "教师密码",required = true),
            @Parameter(name = "t_name",description = "教师名字",required = false),
            @Parameter(name = "college",description = "教师所属学院",required = false)
    })
    @PostMapping("/teacher")
    public R add(@RequestBody Teacher teacher) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
        return  teacherService.add(teacher);
    }

    @Operation(summary = "教师更新数据",description = "提供一整个teacher数据")
    @PutMapping("/teacher/upData")
    public R upData(@RequestBody Teacher teacher) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
        return  teacherService.upDate(teacher);
    }


    @Operation(summary = "教师全表查询",description = "无需提供数据")
    @PostMapping("/teacher/All")
    public List<Teacher> getAllTeachers() throws NoSuchAlgorithmException, InvalidKeySpecException {
        return teacherService.getAllTeachers();
    }

    @Operation(summary = "教师删除",description = "需要提供username")
    @Parameter(name = "username",description = "教师账号",required = true)
    @GetMapping("/teacher/del")
    public R Delect(@RequestParam("username") String username){
        Teacher teacher = new Teacher();
        teacher.setUsername(username);
        return teacherService.Delect((teacher));
    }


    @Operation(summary = "教师单个查询",description = "需要提供username")
    @Parameter(name = "username",description = "教师账号",required = true)
    @PostMapping("/teacher/getTeacher")
    public R getTeacher(@RequestBody Teacher teacher) throws NoSuchAlgorithmException, InvalidKeySpecException {
        return teacherService.getTeacher(teacher);
    }


}
